package com.ccp.dev.system.model;

import com.ccp.dev.core.basic.api.ISysUser;
import com.ccp.dev.core.basic.model.BaseModel;
import com.ccp.dev.core.util.AppUtil;
import com.ccp.dev.core.util.BeanUtils;
import com.ccp.dev.core.util.StringUtil;
import com.ccp.dev.system.service.SysRoleService;
import org.apache.commons.lang3.builder.ToStringBuilder;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import java.util.*;


/**
 * 用户表 Model对象
 * @author y
 */
public class SysUser extends BaseModel implements UserDetails , ISysUser {
	/**
	 * 系统管理员别名
	 */
	public final static String DEFAULT_XTGLY = "xtgly";
	/**
	 * 安全保密管理员别名
	 */
	public final static String DEFAULT_AQBMGLY = "aqbmgly";
	/**
	 * 安全审计管理员别名
	 */
	public final static String DEFAULT_AQSJY = "aqsjy";
	/**
	 * 系统管理员名称
	 */
	public final static String DEFAULT_XTGLY_NAME = "系统管理员";
	/**
	 * 安全保密管理员名称
	 */
	public final static String DEFAULT_AQBMGLY_NAME = "安全保密管理员";
	/**
	 * 安全审计管理员名称
	 */
	public final static String DEFAULT_AQSJY_NAME = "安全审计员";

	/**
	 * 从角色
	 */
	public final static String SEARCH_BY_ROL = "1";
	/**
	 * 从组织
	 */
	public final static String SEARCH_BY_ORG = "2";
	/**
	 * 从岗位
	 */
	public final static String SEARCH_BY_POS = "3";
	/**
	 * 从在线
	 */
	public final static String SEARCH_BY_ONL = "4";
	/**
	 * 从在线
	 */
	public final static String SEARCH_BY_LEADER = "5";
	/**
	 * 默认密级
	 */
	public static final Integer DEFAULT_PRIVACYLEVEL= 1;
	/**
	 * 默认用户密码
	 */
	public final static String DEFAULT_PASS_WORD="12345678!a";
	/**
	 * 默认用户状态描述  正式员工
	 */
	public final static String DEFAULT_USER_STATUS="0";

	/**
	 * 账号未锁定
	 */
	public final static Short UN_LOCKED = 0;
	/**
	 * 账号被锁定
	 */
	public final static Short LOCKED = 1;

	/**
	 * 账号未期
	 */
	public final static Short UN_EXPIRED = 0;

	/**
	 * 账号过期
	 */
	public final static Short EXPIRED = 1;

	/**
	 * 账号激活
	 */
	public final static Short STATUS_OK = 1;

	/**
	 * 账号禁用
	 */
	public final static Short STATUS_NO = 0;
	/**
	 * 账号删除
	 */
	public final static Short STATUS_DEL = -1;
	
	/**
	 * 在线
	 */
	public final static Short ISONLINE_Y = 1;
	
	/**
	 * 非在线
	 */
	public final static Short ISONLINE_N = 0;
	
	/**
	 * 数据来自系统添加
	 */
	public final static Short FROMTYPE_DB=0;
	/**
	 * 数据来自AD同步,并在系统中未被设置
	 */
	public final static Short FROMTYPE_AD=1;
	/**
	 * 数据来自AD同步,并在系统中被设置过
	 */
	public final static Short FROMTYPE_AD_SET=2;
	/**
	 * 是当前用户
	 */
	public final static Short IS_CURRENT = 1;
	/**
	 * 不是当前用户
	 */
	public final static Short NOT_CURRENT = 0;


	/**
	 * 用户id
	 */
	private String userId;
	/**
	 * 姓名
	 */
	private String fullName;
	/**
	 * 帐号
	 */
	private String account;
	/**
	 * 密码
	 */
	private String password;
	/**
	 * 是否过期0未过期 1已过期
	 */
	private Short isExpired;
	/**
	 * 是否锁定 0未锁定 1已锁定
	 */
	private Short isLock;
	/**
	 * 状态 -1 删除  0 禁用 1激活
	 */
	private Short status;
	/**
	 * 邮箱
	 */
	private String email;
	/**
	 * 手机
	 */
	private String mobile;
	/**
	 * 电话
	 */
	private String phone;
	/**
	 * 性别0女1男
	 */
	private String sex;
	/**
	 * 照片
	 */
	private String picture;
	/**
	 * 入职日期
	 */
	private Date entryDate;
	/**
	 * 员工状态 0正式员工,1试用员工,2停薪留职,3返聘,4中转
	 */
	private String userStatus;
	/**
	 * 密码更新时间
	 */
	private Date pwdUpdTime;
	/**
	 * 当前登陆IP
	 */
	private String currIp;
	/**
	 * 是否在线0：非在线；1：在线
	 */
	private Short isOnline;
	/**
	 * 用户密级  1非密
	 */
	private Integer privacyLevel;
	/**
	 * 身份证号
	 */
	private String idCard;
	/**
	 * 用户排序序号(=1,领导	=0,非领导)
	 */
	private Integer sortNumber;
	/**
	 * 用户选择的主页风格
	 */
	private String uiStyle;
	/**
	 * 数据来源
	 */
	private Short fromType;

	/**
	 * 类型
	 */
	private String retype;
	/**
	 * 组织名称
	 */
	private String orgName;

	/**
	 * 组织路径
	 */
	private String orgPathName;
	/**
	 * 用户组织id
	 */
	private String userOrgId;
	/**
	 * 上级id数组
	 */
	private String[] superiorIds;
	/**
	 * 组织id
	 */
	private String orgId;
	/**
	 * 岗位Id数组
	 */
	private String[] posId;
	/**
	 * 临时变量 主要负责的orgid
	 */
	private String isCharge;
	/**
	 * 主要岗位Id
	 */
	private String isPrimary;
	/**
	 * 是否是当前用户
	 */
	private Short isCurrent;
	/**
	 * 角色Id数组
	 */
	private String[] roleId;
	/**
	 * 登录异常次数
	 */
	private Short missNumber;

	/**
	 * 临时变量 主组织id
	 */
	private String primayOrgId;


	public Short getMissNumber() {
		return missNumber;
	}

	public void setMissNumber(Short missNumber) {
		this.missNumber = missNumber;
	}

	public String[] getRoleId() {
        return roleId;
    }

    public void setRoleId(String[] roleId) {
        this.roleId = roleId;
    }

    public String getOrgId() {
		return orgId;
	}

	public void setOrgId(String orgId) {
		this.orgId = orgId;
	}

	public String[] getPosId() {
		return posId;
	}

	public void setPosId(String[] posId) {
		this.posId = posId;
	}

	public String getIsCharge() {
		return isCharge;
	}

	public void setIsCharge(String isCharge) {
		this.isCharge = isCharge;
	}

	public String getIsPrimary() {
		return isPrimary;
	}

	public void setIsPrimary(String isPrimary) {
		this.isPrimary = isPrimary;
	}

	public Integer getSortNumber() {
		return sortNumber;
	}

	public void setSortNumber(Integer sortNumber) {
		this.sortNumber = sortNumber;
	}

	public Short getFromType() {
		return fromType;
	}
	
	public void setFromType(Short fromType) {
		this.fromType = fromType;
	}

	public String getRetype() {
		return retype;
	}

	public void setRetype(String retype) {
		this.retype = retype;
	}

	public String getSex() {
		return sex;
	}

	public void setSex(String sex) {
		this.sex = sex;
	}

	public String getPicture() {
		return picture;
	}

	public void setPicture(String picture) {
		this.picture = picture;
	}

	public String getOrgName() {
		return orgName;
	}

	public void setOrgName(String orgName) {
		this.orgName = orgName;
	}

	public String getOrgPathName() {
		return orgPathName;
	}

	public void setOrgPathName(String orgPathName) {
		this.orgPathName = orgPathName;
	}

	public void setAccount(String account) {
		this.account = account;
	}

	@Override
	public String getAccount() {
		return account;
	}

	public void setPassword(String password) {
		this.password = password;
	}


    @Override
    public String getPassword() {
		return password;
	}

    @Override
    public String getUsername() {
        return userId;
    }

    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        Collection<GrantedAuthority> rtnList= new ArrayList<>();
        SysRoleService sysRoleService =  AppUtil.getBean(SysRoleService.class);
        Collection<String> totalRoleCol=  sysRoleService.getRolesByUserId(userId);
        if(BeanUtils.isNotEmpty(totalRoleCol)){
            for(String role:totalRoleCol){
                rtnList.add(new SimpleGrantedAuthority(role));
            }
        }
        return rtnList;
    }

    @Override
    public boolean isAccountNonExpired() {
        if(isExpired==null){
            return true;
        }
        if (isExpired.shortValue() == UN_EXPIRED.shortValue()) {
            return true;
        } else {
            return false;
        }
    }

    @Override
    public boolean isAccountNonLocked() {
        if(isLock==null){
            return true;
        }
        if (isLock.shortValue() == UN_LOCKED.shortValue()) {
            return true;
        } else {
            return false;
        }
    }

    @Override
    public boolean isCredentialsNonExpired() {
        return true;
    }

    @Override
    public boolean isEnabled() {
        if(StringUtil.isEmpty(status)){
            return true;
        }
        return status.equals(STATUS_OK )? true : false;
    }

    public void setIsExpired(Short isExpired) {
		this.isExpired = isExpired;
	}

	public Short getIsExpired() {
		return isExpired;
	}

	public void setIsLock(Short isLock) {
		this.isLock = isLock;
	}

	public Short getIsLock() {
		return isLock;
	}

	public void setStatus(Short status) {
		this.status = status;
	}

	public Short getStatus() {
		return status;
	}

	public void setEmail(String email) {
		this.email = email;
	}

	public String getEmail() {
		return email;
	}

	public void setMobile(String mobile) {
		this.mobile = mobile;
	}

	public String getMobile() {
		return mobile;
	}

	public void setPhone(String phone) {
		this.phone = phone;
	}

	public String getPhone() {
		return phone;
	}

	public Date getEntryDate() {
		return entryDate;
	}

	public void setEntryDate(Date entryDate) {
		this.entryDate = entryDate;
	}

	public String getIdCard() {
		return idCard;
	}

	public void setIdCard(String idCard) {
		this.idCard = idCard;
	}

	public String getUiStyle() {
		return uiStyle;
	}

	public void setUiStyle(String uiStyle) {
		this.uiStyle = uiStyle;
	}

	public String getUserStatus() {
		return userStatus;
	}

	public void setUserStatus(String userStatus) {
		this.userStatus = userStatus;
	}

	public Date getPwdUpdTime() {
		return pwdUpdTime;
	}

	public void setPwdUpdTime(Date pwdUpdTime) {
		this.pwdUpdTime = pwdUpdTime;
	}

	public String getCurrIp() {
		return currIp;
	}

	public void setCurrIp(String currIp) {
		this.currIp = currIp;
	}

	public Short getIsOnline() {
		return isOnline;
	}

	public void setIsOnline(Short isOnline) {
		this.isOnline = isOnline;
	}

	public Integer getPrivacyLevel() {
		return privacyLevel;
	}

	public void setPrivacyLevel(Integer privacyLevel) {
		this.privacyLevel = privacyLevel;
	}

	public Short getIsCurrent() {
		return isCurrent;
	}

	public void setIsCurrent(Short isCurrent) {
		this.isCurrent = isCurrent;
	}

	@Override
	public String getUserId() {
		return userId;
	}

	public void setUserId(String userId) {
		this.userId = userId;
	}
	@Override
	public String getFullName() {
		return fullName;
	}

	public void setFullName(String fullName) {
		this.fullName = fullName;
	}

	public void setFromType(short fromType) {
		this.fromType = fromType;
	}

	public String getUserOrgId() {
		return userOrgId;
	}

	public void setUserOrgId(String userOrgId) {
		this.userOrgId = userOrgId;
	}

	public String[] getSuperiorIds() {
		return superiorIds;
	}

	public void setSuperiorIds(String[] superiorIds) {
		this.superiorIds = superiorIds;
	}

	@Override
	public boolean equals(Object rhs) {

		if (rhs instanceof SysUser) {
			return this.userId.equals(((SysUser) rhs).userId);
		}
		return false;
	}

	@Override
	public int hashCode() {
		int result = Objects.hash(account);
		return result;
	}

	@Override
	public String toString() {
		return new ToStringBuilder(this)
				.append("userId", userId)
				.append("fullName", fullName)
				.append("account", account)
				.append("password", password)
				.append("isExpired", isExpired)
				.append("isLock", isLock)
				.append("status", status)
				.append("email", email)
				.append("mobile", mobile)
				.append("phone", phone)
				.append("sex", sex)
				.append("picture", picture)
				.append("entryDate", entryDate)
				.append("userStatus", userStatus)
				.append("pwdUpdTime", pwdUpdTime)
				.append("currIp", currIp)
				.append("isOnline", isOnline)
				.append("privacyLevel", privacyLevel)
				.append("idCard", idCard)
				.append("sortNumber", sortNumber)
				.append("uiStyle", uiStyle)
				.append("fromType", fromType)
				.append("retype", retype)
				.append("orgName", orgName)
				.append("orgPathName", orgPathName)
				.append("userOrgId", userOrgId)
				.append("superiorIds", superiorIds)
				.append("orgId", orgId)
				.append("posId", posId)
				.append("isCharge", isCharge)
				.append("isPrimary", isPrimary)
				.append("roleId", roleId)
				.append("missNumber", missNumber)
				.toString();
	}

	public String getPrimayOrgId() {
		return primayOrgId;
	}

	public void setPrimayOrgId(String primayOrgId) {
		this.primayOrgId = primayOrgId;
	}
}